﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.UTIL;
using CRM.WEB.CommonClass;

namespace CRM.WEB.Ali
{
    public partial class ExportTakeEffectData : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtStartDate.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                txtEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtStartDate.Text))
                {
                    ShowAlert("开始时间不能为空！");
                    return;
                }
                if (string.IsNullOrEmpty(txtEndDate.Text))
                {
                    ShowAlert("结束时间不能为空！");
                    return;
                }
                string ids = string.Empty;
                string error = string.Empty;
                int asCount, afCount, usCount, ufCount, total;

                CommonClass.Common.GetTakeEffectData(ConfigurationManager.AppSettings["SupplierId"],
                    Convert.ToDateTime(txtStartDate.Text), Convert.ToDateTime(txtEndDate.Text), ref ids, ref error,
                    out asCount, out afCount,
                    out usCount, out ufCount, out total, false);


                if (string.IsNullOrEmpty(ids))
                {
                    ShowAlert("该时间段内没有生效的订单信息！");
                    return;
                }

                txtInfo.Text = string.Format("生效订单总数量：{5}\r\n新增成功数量： {0}\r\n新增失败数量：{1}\r\n更新成功数量：{2}\r\n更新失败数量：{3}\r\n{4}",
                    asCount,
                    afCount, usCount, ufCount, string.IsNullOrEmpty(error) ? string.Empty : error, total);


                DataTable dt =
                    new DBChannel().GetDataBySql(string.Format("SELECT * FROM Ali_Orders WHERE OrderID in ({0})", ids));
                dt.TableName = "OrderList";

                List<DataTable> dtSources = new List<DataTable>();
                dtSources.Add(dt);

                Dictionary<string, object> dicVariables = new Dictionary<string, object>();
                //dicVariables.Add("year", DateTime.Now.Year.ToString() + "年");
                //dicVariables.Add("DepName", "");

                string excelTemplatePath = Server.MapPath("\\").Trim('\\') +
                                           "\\UploadFile\\Template\\TakeEffectDataTemplate.xls";

                string fileName = "生效数据-(" + DateTime.Now.ToString("yyyyMMdd") + ")";
                ExcelHelper.ExportToExcel(excelTemplatePath, dicVariables, dtSources, fileName);

            }
            catch (Exception ex)
            {
                ShowAlert(ex.Message);
            }
        }

        protected void btnSyncData_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtStartDate.Text))
                {
                    ShowAlert("开始时间不能为空！");
                    return;
                }
                if (string.IsNullOrEmpty(txtEndDate.Text))
                {
                    ShowAlert("结束时间不能为空！");
                    return;
                }
                string ids = string.Empty;
                string error = string.Empty;
                int asCount, afCount, usCount, ufCount, total;

                CommonClass.Common.GetTakeEffectData(ConfigurationManager.AppSettings["SupplierId"],
                    Convert.ToDateTime(txtStartDate.Text), Convert.ToDateTime(txtEndDate.Text), ref ids, ref error,
                    out asCount, out afCount,
                    out usCount, out ufCount, out total, true);


                ////if (string.IsNullOrEmpty(ids))
                ////{
                ////    ShowAlert("该时间段内没有生效的订单信息！");
                ////    return;
                ////}

                txtInfo.Text = string.Format("生效订单总数量：{5}\r\n新增成功数量： {0}\r\n新增失败数量：{1}\r\n更新成功数量：{2}\r\n更新失败数量：{3}\r\n{4}",
                    asCount,
                    afCount, usCount, ufCount, string.IsNullOrEmpty(error) ? string.Empty : error, total);


                //DataTable dt =
                //    new DBChannel().GetDataBySql(string.Format("SELECT * FROM Ali_Orders WHERE OrderID in ({0})", ids));
                //dt.TableName = "OrderList";

                //List<DataTable> dtSources = new List<DataTable>();
                //dtSources.Add(dt);

                //Dictionary<string, object> dicVariables = new Dictionary<string, object>();
                ////dicVariables.Add("year", DateTime.Now.Year.ToString() + "年");
                ////dicVariables.Add("DepName", "");

                //string excelTemplatePath = Server.MapPath("\\").Trim('\\') +
                //                           "\\UploadFile\\Template\\TakeEffectDataTemplate.xls";

                //string fileName = "生效数据-(" + DateTime.Now.ToString("yyyyMMdd") + ")";
                //ExcelHelper.ExportToExcel(excelTemplatePath, dicVariables, dtSources, fileName);

                //Page.ClientScript.RegisterStartupScript(this.GetType(), "JS", "mini.hideMessageBox('msg');");
            }
            catch (Exception ex)
            {
                ShowAlert(ex.Message);
            }
        }
    }
}